package com.java78.addon.journal.controller;

import com.java78.addon.journal.model.JpressAddonJournal;
import com.java78.addon.journal.service.JpressAddonJournalService;
import com.jfinal.aop.Inject;
import com.jfinal.kit.Ret;

import io.jboot.web.controller.annotation.RequestMapping;
import io.jboot.web.validate.EmptyValidate;
import io.jboot.web.validate.Form;
import io.jpress.web.base.TemplateControllerBase;

@RequestMapping("/journal/view")
public class JpressAddonJournalViewController extends TemplateControllerBase {
	
	@Inject
	private JpressAddonJournalService service;
	
	public void index() {

        int page = getParaToInt("page", 1);
        setAttr("page", page);

        setMenuActive(menu -> menu.isUrlStartWidth("/journal/view"));
        render("journal.html");
    }
	
	@EmptyValidate({@Form(name = "type"),@Form(name = "reClick")})
	public void click() {
		Long id = getIdPara();
    	Object type = getParaSet("type");//获取点击类型
    	Object reClick = getParaSet("reClick");//获取点击类型
		JpressAddonJournal entry = service.findById(id);
		int count = 0;
		int addNum = 0;
		if(reClick!= null && reClick.toString().indexOf("second")!=-1) {
			addNum = -1;
		}else if(reClick!= null && reClick.toString().indexOf("first")!=-1) {
			addNum = 1;
		}
    	if(type !=null && entry != null) {
    		if(type.toString().indexOf("catcallCount")!=-1) {
    	    	entry.setCatcallCount(entry.getCatcallCount() == null ? 1 : entry.getCatcallCount()+addNum);
    	    	count = entry.getCatcallCount();
    		}else {
    			entry.setThumbsupCount(entry.getThumbsupCount() == null ? 1 : entry.getThumbsupCount()+addNum);	
    			count = entry.getThumbsupCount();
    	    }
    		service.saveOrUpdate(entry);
    	}
    	renderJson(Ret.ok().set("count",count));
    }
}
